import datastructure.linklist.ListNode;

/**
 * @Author ZhangCuirong
 * @Date 2025/8/13 9:09
 * @description:
 */
public class Solution24 {
    public ListNode swapPairs(ListNode head) {
        if (head == null || head.next == null) {
            return head;
        }
        ListNode dummy = new ListNode(-1, head);
        ListNode pre = dummy;
        ListNode cur = head;
        ListNode next = head.next;
        int count = 0;
        while (cur != null && next != null) {
            if (count % 2 == 0) {
                ListNode temp = next.next;
                pre.next = next;
                next.next = cur;
                cur.next = temp;
                pre = next;
                next = temp;
            } else {
                pre = pre.next;
                cur = cur.next;
                next = next.next;
            }
            count++;
        }
        return dummy.next;
    }

    public static void main(String[] args) {
        Solution24 solution = new Solution24();
        ListNode head =ListNode.arrayToList(new int []{1,2,3,4});
        ListNode newHead = solution.swapPairs(head);
        ListNode.printList(newHead);
    }
}
